package com.example.transaction.config;

import com.example.transaction.model.Account;
import com.example.transaction.repository.AccountRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Component
@RequiredArgsConstructor
@Slf4j
public class DataInitializer implements CommandLineRunner {

    private final AccountRepository accountRepository;

    @Override
    @Transactional
    public void run(String... args) {
        log.info("Initializing test data...");
        
        // 创建测试账户
        if (accountRepository.count() == 0) {
            log.info("Creating test accounts");
            
            accountRepository.save(new Account("ACC001", "张三", new BigDecimal("1000.00")));
            accountRepository.save(new Account("ACC002", "李四", new BigDecimal("2000.00")));
            accountRepository.save(new Account("ACC003", "王五", new BigDecimal("3000.00")));
            accountRepository.save(new Account("ACC004", "赵六", new BigDecimal("4000.00")));
            
            log.info("Test accounts created successfully");
        } else {
            log.info("Test accounts already exist");
        }
    }
}